﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Circle.Domain.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Circle.Data.Mapping;
using Circle.Data.Migration;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using System.Data.Entity.ModelConfiguration;
using Circle.Domain.Interface;

namespace Circle.Data
{


    public class CircleDbContext : DbContext, ICircleDbContext
    {
        static CircleDbContext()
        {
            //DropCreateDatabaseIfModelChanges, CreateDatabaseIfNotExists, DropCreateDatabaseAlways         
            Database.SetInitializer<CircleDbContext>(new CreateDatabaseIfNotExists<CircleDbContext>());
        }

        public CircleDbContext()
            : base("CircleDbContext")
        {
            Configuration.LazyLoadingEnabled = false;
        }

        public DbSet<Setting> Settings { get; set; }

        public DbSet<LocaleLanguage> Languages { get; set; }
        public DbSet<LocaleResourceKey> LocaleResourceKeys { get; set; }
        public DbSet<LocaleResourceValue> LocaleResourceValues { get; set; }
        public DbSet<ContentVote> Votes { get; set; }
        public DbSet<ContentBannedWord> BannedWords { get; set; }
        public DbSet<MemberUser> Users { get; set; }
        public DbSet<MemberRole> Roles { get; set; }
        public DbSet<UploadFileCategory> UploadFileCategories { get; set; }
        public DbSet<UploadFile> UploadFiles { get; set; }

        public DbSet<ContentTopic> Topics { get; set; }
        public DbSet<ContentComment> Comments { get; set; }

        public DbSet<Permission> Permissions { get; set; }
        public DbSet<PermissionOnThread> PermissionOnThreads { get; set; }
    


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //make configuration seed execute
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<CircleDbContext, CircleConfiguration>());

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


           
            
            //Setting
            modelBuilder.Configurations.Add(new SettingMapping());

            //Resource
            modelBuilder.Configurations.Add(new LocaleLanguageMapping());
            modelBuilder.Configurations.Add(new LocaleResourceKeyMapping());
            modelBuilder.Configurations.Add(new LocaleResourceValueMapping());

            //Membership
            modelBuilder.Configurations.Add(new MemberRoleMapping());
            modelBuilder.Configurations.Add(new MemberUserMapping());

            //Content
            modelBuilder.Configurations.Add(new ContentTopicMapping());
            modelBuilder.Configurations.Add(new ContentCommentMapping());

            modelBuilder.Configurations.Add(new ContentVoteMapping());
            modelBuilder.Configurations.Add(new ContentBannedWordMapping());

            //File
            modelBuilder.Configurations.Add(new UploadFileMapping());
            modelBuilder.Configurations.Add(new UploadFileCategoryMapping());

            //Permssion
            modelBuilder.Configurations.Add(new PermissionMapping());
            modelBuilder.Configurations.Add(new PermissionOnThreadMapping());

            base.OnModelCreating(modelBuilder);
        }
    }
}
